<template>
  <el-dialog :title="info.title" :visible.sync="visible" center width="460px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="100px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="24">
          <el-form-item label="用户名称" prop="su_id">
            <el-select
              v-model="form.su_id"
              size="small"
              :remote-method="selUser"
              remote
              filterable
              style="width: 100%"
              placeholder="请选择员工"
              clearable
              @focus="selUser"
            >
              <el-option
                v-for="item in users"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="部门角色" prop="role_id">
            <el-select v-model="form.role_id" size="small" style="width: 100%">
              <el-option
                v-for="item in roles"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleEdit()">保 存</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { queryOrganChildren, selUser, queryUser, updStaff, addStaff } from '@/api/organ'

export default {
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      form: {},
      users: [],
      roles: [],
      rules: {
        su_id: [{ required: true, trigger: 'blur', message: '请选择用户' }],
        role_id: [{ required: true, trigger: 'blur', message: '请选择部门角色' }]
      },
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.form = this.info.edit
        this.queryRoles()
        if (this.info.type === 'edit') {
          this.user()
        }
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    queryRoles(){
      queryOrganChildren({
        'parent_id': this.form.dept_id
      }).then(response => {
        this.roles = response.list
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    selUser(query) {
      let str = ''
      if (Object.prototype.toString.call(query) === '[object String]') str = query
      selUser({ name: str }).then(response => {
        this.users = response.list
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    user() {
      this.$set(this, 'users', [])
      queryUser({ id: this.form.su_id }).then(response => {
        const info = {}
        info.key = response.user.su_id
        info.value = response.user.nickname
        this.users.push(info)
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    handleEdit() {
      this.$refs['formDate'].validate(async(valid) => {
        if (valid) {
          if (this.info.type === 'add') {
            addStaff(this.form).then(response => {
              this.getData()
              this.handleCancel()
              this.$message.success('添加成功')
            }).catch(err => {
              this.$message.error(err.msg)
            })
          } else {
            updStaff(this.form).then(response => {
              this.getData()
              this.handleCancel()
              this.$message.success('编辑成功')
            }).catch(err => {
              this.$message.error(err.msg)
            })
          }
        }
      })
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style>

</style>